#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/wait.h>

#define SYN 0
#define ACK 1
#define SYNACK 2
#define FIN 3
#define RST 4
#define DATA 5

struct tcp_packet
{
	unsigned short port_source;
	unsigned short port_destination;
	unsigned numero_sequence;
	unsigned numero_ack;
	unsigned short window;
	//unsigned short crc;
	char syn;
	char fin;
	char ack;
	char rst;
	char data [1000];
};

typedef enum
{
	CLOSED,
	SYNSENT,
	SYNRECEIVED,
	ESTABLISHED
} EtatTcp;

struct connexion{
	EtatTcp etat;
	char window [1000];
	unsigned num_seq;
	unsigned short port_serveur;
	unsigned short port_client;
	unsigned long adresse_serveur;
	unsigned long adresse_client;
};

int MonAccept(int sockfd,struct sockaddr *addr,socklen_t *addrlen);
int MonConnect(int sockfd, struct sockaddr_in *addr);
int MonListen(int sockfd, int backlog);
ssize_t MonRead(int fd, void *buf, size_t count);
ssize_t MonWrite(int fd, const void *buf, size_t count);
int MonClose(int fd);
struct tcp_packet creerPaquetTCP(unsigned short port_source, unsigned short port_destination, unsigned numero_sequence, unsigned numero_ack, unsigned short window, int flag_OPT, char message[1000]);
int typePaquet(struct tcp_packet paquetAnalyse);
int creerSocket(char ipadrr[15],int port);
void afficherInfosConnexion();
void modifierEtatTCP(EtatTcp flag_OPT);
struct sockaddr_in * creerSockAddr(unsigned short port, char *arg, int flag, unsigned long addr);
void afficherInfosPaquet(struct tcp_packet paquetAnalyse);
int closeServeur(int fd);
int testEtatTCP();